/**
 * Class Bootstrap
 * is used to load scripts and image assets
 */
Bootstrap = (function() {
	/**
	 * Bootstrap constructor
	 * @constructor
	 */
	function Bootstrap() {
		this.local = new Local('bootstrap');
		this.head = new View(document.head);
		this.counter = 0;
		this.scriptsCounter = 0;
		this.assetsCounter = 0;
	}
	/**
	 * Start loading scripts and assets from bootstrap.json file or form saved in localstorage
	 * @param [update] {Boolean} to get files form json file or form localstorage
	 */
	Bootstrap.prototype.load = function(update) {
		var _this = this;
		this.data = this.local.get();
		if (update || !this.data) {
			this.ajax = new Ajax();
			this.ajax.setOnSuccessListener(function(data) {
				if (data.status === "success") {
					_this.local.set(_this.data = data.data);
				}
				_this.loadScripts();
				_this.loadAssets();
			});
			this.ajax.open("bootstrap.json");
			this.ajax.send();
		} else {
			this.loadScripts();
			this.loadAssets();
		}
	};
	/**
	 * Load Scripts files
	 */
	Bootstrap.prototype.loadScripts = function() {
		var script,
			_this = this;
		script = new View(document.createElement('script'));
		script.element.onload = function(e) {
			_this.scriptsCounter++;
			_this.update();
			if (_this.scriptsCounter < _this.data.scripts.length) {
				_this.loadScripts();
			} else {
				if (_this.assetsCounter === _this.data.assets.length) {
					_this.ready();
				}
			}
		};
		script.attr({
			type: "text/javascript"
		});
		if (this.data.scripts[this.scriptsCounter]) {
			script.element.src = this.data.scripts[this.scriptsCounter].src;
		}
		this.head.append(script);
	};
	/**
	 * Load image assets
	 */
	Bootstrap.prototype.loadAssets = function() {
		var asset, image, _i, _len, _ref,
			_this = this;
		if (this.data.assets.length === 0 && this.scriptsCounter === this.data.scripts.length) {
			this.ready();
		}
		_ref = this.data.assets;
		for (_i = 0, _len = _ref.length; _i < _len; _i++) {
			asset = _ref[_i];
			image = new Image();
			image.onload = function() {
				_this.assetsCounter++;
				_this.update();
				if (_this.assetsCounter === _this.data.assets.length && _this.scriptsCounter === _this.data.scripts.length) {
					return _this.ready();
				}
			};
			image.src = asset;
		}
	};
	/**
	 * Update counter
	 */
	Bootstrap.prototype.update = function() {
		this.counter++;
		if(this.updateListener)
			this.updateListener.call(this);
	};
	/**
	 * This method is called when all scripts and assets are loaded
	 */
	Bootstrap.prototype.ready = function() {
		if(this.readyListener)
			this.readyListener.call(this);
	};
	/**
	 * Set on update listener
	 * @param listener {Function} update listener
	 */
	Bootstrap.prototype.setOnUpdateListener = function(listener){
		if(!(listener instanceof Function)) throw "listener is not a Function";
		this.updateListener = listener;
	};
	/**
	 * Set on ready listener
	 * @param listener {Function} ready listener
	 */
	Bootstrap.prototype.setOnReadyListener = function(listener) {
		if(!(listener instanceof Function)) throw "listener is not a Function";
		this.readyListener = listener;
	};

	return Bootstrap;

})();
